using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;

namespace WizardSample
{
    /// <summary>
    /// 
    /// </summary>
    public class ButtonEnabledConverter : IMultiValueConverter
    {
        /*
        public object Convert(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is Int32)
            {
                return ((Int32) value > 0) ? false : true;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is bool)
            {
                return (Boolean) value ? 1 : 0;
            }
            return null;
        }
        */
        public object Convert(object [] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (
                values [0] != null && values [0] is Int32
                && values [1] != null && values [1] is Int32)
            {
                bool bValue = ((Int32) values [0] > 0 && (Int32) values [1] > 0);
                // ((Int32)values[0] > 0 && (Int32)values[1] > 0) ? true : false;
                return bValue;
            }
            return null;
        }

        public object [] ConvertBack(object value, Type [] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public partial class WizardPageAuthFarm : WizardPageBase
    {
        public WizardPageAuthFarm()
        {
            InitializeComponent();
            this.CommandAddNewFarmServer =
                new DelegateCommand<Object>(OnAddNewFarmServerCommand);
            this.CommandRemoveFarmServer =
                new DelegateCommand<Object>(OnRemoveFarmServerCommand, CanRemoveFarmServerCommand);
            this.CommandEditFarmServer =
                new DelegateCommand<Object>(OnEditFarmServerCommand, CanEditFarmServerCommand);
        }

        /// <summary>
        /// 
        /// </summary>
        public ICommand CommandAddNewFarmServer
        {
            get; private set;
        }

        public ICommand CommandRemoveFarmServer
        {
            get; private set;
        }

        public ICommand CommandEditFarmServer
        {
            get; private set;
        }

        private NewServerDialog CreateNewServerDialog(String caption, String serverName)
        {
            NewServerDialog dialog =
                new NewServerDialog()
                {
                    ServerName = serverName,
                    Title = caption
                    // ,Owner = this.WizardWindow.Parent
                };

            dialog.Left =
                this.WizardWindow.Left +
                this.WizardWindow.Width / 2 - dialog.Width / 2;
            dialog.Top =
                this.WizardWindow.Top +
                this.WizardWindow.Height / 2 - dialog.Height / 2;

            return dialog;
        }

        /// <summary>
        /// Command Handlers
        /// </summary>
        private void OnAddNewFarmServerCommand(Object sender)
        {
            String server = sender as String;
            /*
            Programme.EventAggregator.GetEvent<CommandNotificationEvent>()
                .Publish(new KeyValuePair<String, Object>(@"AddServer_Command", sender));
            */
            NewServerDialog dialog = CreateNewServerDialog(@"Add Farm Server", String.Empty);

            Boolean? dialogResult = dialog.ShowDialog();

            if (dialogResult.Value)
            {
                AuthenticationWizardData wizardData =
                    this.DataContext as AuthenticationWizardData;
                wizardData.AddFarmServer(dialog.ServerName);
            }
        }

        private void OnRemoveFarmServerCommand(Object sender)
        {
            String server = sender as String;
            /*
            AuthenticationForm.EventAggregator.GetEvent<CommandNotificationEvent>()
                .Publish(@"CreateNewSite_Command");
            */
            AuthenticationWizardData wizardData =
                this.DataContext as AuthenticationWizardData;
            wizardData.RemoveFarmServer(server);
        }

        private bool CanRemoveFarmServerCommand(Object sender)
        {
            return true;
            /*
             * See Condition below            
            */
        }

        private bool CanEditFarmServerCommand(Object sender)
        {
            return true;
            /*
            AuthenticationWizardData wizardData =
                this.DataContext as AuthenticationWizardData;
            return (
                wizardData != null &&
                wizardData.FarmServers.Count > 0 &&
                this.ctrl_serverList.SelectedItems.Count > 0);
            */
        }

        private void OnEditFarmServerCommand(Object sender)
        {
            String server = sender as String;
            /*
            Programme.EventAggregator.GetEvent<CommandNotificationEvent>()
                .Publish(new KeyValuePair<String, Object>(@"EditServer_Command", sender));
            */
            NewServerDialog dialog = CreateNewServerDialog(@"Edit Farm Server", server);

            Boolean? dialogResult = dialog.ShowDialog();

            if (dialogResult.Value)
            {
                AuthenticationWizardData wizardData =
                    this.DataContext as AuthenticationWizardData;
                wizardData.EditFarmServer(server, dialog.ServerName);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void BindToData(Object contextData)
        {
            AuthenticationWizardData wizardData = contextData as AuthenticationWizardData;

            // Bind wizard data
            if (wizardData != null)
            {
                this.DataContext = wizardData;
                this.ctrl_transportTypeList.ItemsSource = AuthenticationWizardData.TransportList;
                this.ctrl_transportTypeList.SelectedItem = wizardData.FarmTransport;
                this.ctrl_serverList.ItemsSource = wizardData.FarmServers;
            }
        }

        private void TransportTypeList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.ctrl_sslRelayPort.IsEnabled =
                "SSL Relay" ==
                (this.ctrl_transportTypeList.SelectedItem as String);
        }
    }
}